﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace DbSharp.Framework.Driver
{
    public class TestDataSourceFactory
    {
        public ITestDataSource SelectDbTestPlugin(string providerName, string connectionString)
        {
            ITestDataSource testSource = null;
            providerName = providerName.Trim();
            if (string.Equals(providerName, "SYSTEM.DATA.ORACLECLIENT", StringComparison.OrdinalIgnoreCase))
            {
                testSource = new OracleTestPlugin();
            }
            else if (string.Equals(providerName, "SYSTEM.DATA.SQLCLIENT", StringComparison.OrdinalIgnoreCase))
            {
                testSource = new SqlServerTestPlugin();
            }
            else if (string.Equals(providerName, "MySql.Data.MySqlClient", StringComparison.OrdinalIgnoreCase))
            {
                testSource = new MySqlTestPlugin();
            }
            else if (string.Equals(providerName, "Devart.Data.Oracle", StringComparison.OrdinalIgnoreCase))
            {
                testSource = new OracleTestPlugin2();
            }
            else if (string.Equals(providerName, "Npgsql", StringComparison.OrdinalIgnoreCase))
            {
                testSource = new PostgresqlTestPlugin();
            }
            else if (string.Equals(providerName, "Microsoft.Jet.OLEDB.4.0", StringComparison.OrdinalIgnoreCase))
            {
                var builder = new OleDbConnectionStringBuilder(connectionString);
                object extendedProperties;
                int index;
                if (builder.TryGetValue("Extended Properties", out extendedProperties))
                {
                    string xproperty = extendedProperties.ToString().Trim().ToUpper();
                    index = xproperty.IndexOf(@"Excel", StringComparison.OrdinalIgnoreCase);
                    if (index > -1)
                    {
                        testSource = new ExcelOleTestPlugin();
                    }
                }
            }
            else if (string.Equals(providerName, "Microsoft.ACE.OLEDB.12.0", StringComparison.OrdinalIgnoreCase))
            {
                var builder = new OleDbConnectionStringBuilder(connectionString);
                object extendedProperties;
                int index;
                if (builder.TryGetValue("Extended Properties", out extendedProperties))
                {
                    string xproperty = extendedProperties.ToString().Trim().ToUpper();
                    index = xproperty.IndexOf(@"Excel", StringComparison.OrdinalIgnoreCase);
                    if (index > -1)
                    {
                        testSource = new Excel2007OleTestPlugin();
                    }
                }
            }
            else
            {
                throw new NotImplementedException("This Db is not implemented yet.");
            }
            return testSource;
        }
    }

}
